package com.site.servlet;

import com.alibaba.fastjson.JSON;
import com.site.dao.ReceiveDao;
import com.site.dao.impl.ReceiveDaoImpl;
import com.site.pojo.Receive;
import com.site.service.ReceiveService;
import com.site.service.impl.ReceiveServiceImpl;
import com.site.utils.CommonResult;
import com.site.utils.FileNameUtil;
import jxl.write.DateTime;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

@WebServlet("/receive.do")
public class ReceiveServlet extends HttpServlet {
    private ReceiveService receiveService = new ReceiveServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        String opr = request.getParameter("opr");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            switch (opr){
                case "addReceive":{
                    out.write(JSON.toJSONString(addReceive(request,response)));
                    break;
                }
                case "getAll":{
                    out.write(JSON.toJSONString(getAll(request,response)));
                    break;
                }
                case "deleteReceive":{
                    out.write(JSON.toJSONString(deleteReceive(request,response)));
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.flush();
            out.close();
        }
    }
    public CommonResult deleteReceive(HttpServletRequest request, HttpServletResponse response)throws Exception{
        CommonResult result = null;
        Long rid = Long.parseLong(request.getParameter("rid"));
        boolean flag = receiveService.deleteByrid(rid);
        if(flag){
            result = CommonResult.success(null);
        }else {
            result = CommonResult.failed();
        }
        return result;
    }
    public CommonResult addReceive(HttpServletRequest request, HttpServletResponse response)throws Exception{
        CommonResult result = null;
        Receive receive = new Receive();
        Long goodsId =Long.parseLong(request.getParameter("goodsId"));
        int receiveCount = Integer.parseInt(request.getParameter("receiveCount"));
        receive.setGoodsId(goodsId);
        receive.setReceiveCount(receiveCount);
        Date date = new Date();
        receive.setOprTime(java.sql.Timestamp.valueOf("2021-12-02 10:48:39"));
        receive.setEid(Long.parseLong("3"));
        String receiveNUmber = FileNameUtil.makeFileName("SDC");
        receive.setReceiveNumber(receiveNUmber);
        boolean flag = receiveService.addReceive(receive);
        ServletContext application = request.getServletContext();
        Receive receive1 = receiveService.seleteByNumber(receiveNUmber);
        receive.setRid(receive1.getRid());
        if(flag){
            result = CommonResult.success(null);
            application.setAttribute("detail","true");
            application.setAttribute("receive",receive);
        }else {
            result = CommonResult.failed();
        }
        return result;
    }
    public CommonResult getAll(HttpServletRequest request, HttpServletResponse response)throws  Exception{
        CommonResult result = null;
        List<Receive> list = receiveService.getAllReceive();
        if(list.size()!=0){
            result = CommonResult.success(list);
        }else {
            result = CommonResult.failed();
        }
        return result;
    }
}
